#region Using

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;

using Gizmox.WebGUI.Common;
using Gizmox.WebGUI.Forms;
using Library.Entity;
using Library.Services;
using System.Collections;

#endregion

namespace Library
{
    public partial class UserControlBookBorrow : UserControl
    {
        BookTransactionService m_BookTransactionService = new BookTransactionService();
        MemberService m_MemberService = new MemberService();
        BookService m_BookService = new BookService();
        User m_currentUser;
        public UserControlBookBorrow(string name, User currentUser, MainForm mainForm)
        {
            InitializeComponent();
            this.Name = name;
            this.Text = name;
            labelTitle.Text = name;
            m_currentUser = currentUser; 
            initColumn();
            initData();
            initButtonClick();
            toolBarButtonDelete.Enabled = listView.SelectedItem != null;
            toolBarButtonReturn.Enabled = listView.SelectedItem != null;
        }

        private void initButtonClick()
        {
            toolBarButtonAddNew.Click += new EventHandler(toolBarButtonAddNew_Click);
            toolBarButtonDelete.Click += new EventHandler(toolBarButtonDelete_Click);
            toolBarButtonRefresh.Click += new EventHandler(toolBarButtonRefresh_Click);
            toolBarButtonReturn.Click += new EventHandler(toolBarButtonReturn_Click);
        }

        void toolBarButtonReturn_Click(object sender, EventArgs e)
        {
            if (listView.SelectedItem != null)
            {
                BookTransaction tr = (BookTransaction)listView.SelectedItem.Tag;
                MessageBox.Show("Are you sure to return this Book [" + tr.Book.Name + "]?", "Question",
                    MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2, new EventHandler(ReturnBook));
            }
        }

        void ReturnBook(object sender, EventArgs e)
        {
            try
            {
                MessageBoxWindow msg = (MessageBoxWindow)sender;
                if (msg.DialogResult == DialogResult.Yes)
                {
                    BookTransaction trBorrow = (BookTransaction)listView.SelectedItem.Tag;
                    if (trBorrow.Id > 0)
                    {
                        if (m_BookTransactionService.IsAlreadyReturn(trBorrow))
                            MessageBox.Show("Book already return.", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1);
                        else
                        {
                            BookTransaction trReturn = new BookTransaction();
                            trReturn.CreatedUserId = m_currentUser.Id;
                            trReturn.ModifiedUserId = m_currentUser.Id;
                            trReturn.BookId = trBorrow.BookId;
                            trReturn.BorrowId = trBorrow.Id;
                            trReturn.MemberId = trBorrow.MemberId;
                            trReturn.TransactionDate = DateTime.Today;
                            trReturn.ReturnDate = DateTime.Today;
                            trReturn.TransactionType = TransactionType.Return;
                            trReturn.Quantity = trBorrow.Quantity;
                            m_BookTransactionService.InsertNewBookTransaction(trReturn);
                            MessageBox.Show("Book has been return.", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1);
                            //listView.Items.Remove(listView.SelectedItem);
                        }
                    }
                }
            }
            catch (Exception x)
            {
                MessageBox.Show(x.Message, "Error Exception", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1);
            }
        }

        void toolBarButtonRefresh_Click(object sender, EventArgs e)
        {
            initData();
        }

        void toolBarButtonDelete_Click(object sender, EventArgs e)
        {
            if (listView.SelectedItem != null)
            {
                MessageBox.Show("Are you sure to delete?", "Question", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2, new EventHandler(DeleteData));
            }
        }

        void DeleteData(object sender, EventArgs e) 
        {
            try
            {
                MessageBoxWindow msg = (MessageBoxWindow)sender;
                if (msg.DialogResult == DialogResult.Yes)
                {
                    BookTransaction trBorrow = (BookTransaction)listView.SelectedItem.Tag;
                    if (trBorrow.Id > 0)
                    {
                        if (m_BookTransactionService.IsAlreadyReturn(trBorrow))
                            MessageBox.Show("Book has been return can not delete.", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1);
                        else
                        {
                            m_BookTransactionService.DeleteBookTransactionBorrow(trBorrow);
                            MessageBox.Show("Record has been deleted.", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1);
                            listView.Items.Remove(listView.SelectedItem);
                        }
                    }
                }
            }
            catch (Exception x)
            {
                MessageBox.Show(x.Message, "Error Exception", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1);
            }
        }

        void toolBarButtonAddNew_Click(object sender, EventArgs e)
        {
            FormBookBorrow frm = new FormBookBorrow(new BookTransaction(), m_currentUser, this);
            frm.ShowDialog();
        }
       
        private void initColumn()
        {
            listView.Columns.Add("Code", 67, HorizontalAlignment.Left);
            listView.Columns.Add("Transaction Date", 92, HorizontalAlignment.Left);
            listView.Columns.Add("Member", 164, HorizontalAlignment.Left);
            listView.Columns.Add("Title", 246, HorizontalAlignment.Left);
            listView.Columns.Add("Transaction Type", 94, HorizontalAlignment.Left);
            listView.Columns.Add("Qty", 29, HorizontalAlignment.Right);
            listView.Columns.Add("Remarks", 200, HorizontalAlignment.Left);
            listView.Columns.Add("Returned", 60, HorizontalAlignment.Left);
        }

        private void initData()
        {
            listView.Items.Clear();
            ArrayList data = new ArrayList();
            if (checkBoxCode.Checked)
            {
                if (checkBoxDAte.Checked)
                    data = (ArrayList)m_BookTransactionService.GetAllBookTransactionByTransactionTypeMember(TransactionType.Borrow, dateTimePickerTrDate.Value, textBoxBookCode.Text);
                else
                    data = (ArrayList)m_BookTransactionService.GetAllBookTransactionByTransactionTypeMember(TransactionType.Borrow, textBoxBookCode.Text);
            }
            else if (checkBoxDAte.Checked)
                data = (ArrayList)m_BookTransactionService.GetAllBookTransactionByTransactionType(TransactionType.Borrow, dateTimePickerTrDate.Value);
            foreach(BookTransaction BookStock in data)
                AddNewData(BookStock);
        }

        internal void AddNewData(BookTransaction trans)
        {
            trans.Member = m_MemberService.GetMemberById(trans.MemberId);
            trans.Book = m_BookService.GetBookById(trans.BookId);
            ListViewItem item = listView.Items.Add(trans.Code);
            item.SubItems.Add(trans.TransactionDate.ToString("dd-MMM-yyyy"));
            item.SubItems.Add(trans.Member.ToString());
            item.SubItems.Add(trans.Book.ToString());
            item.SubItems.Add(trans.TransactionType.ToString());
            item.SubItems.Add(trans.Quantity.ToString());
            item.SubItems.Add(trans.Remark);
            item.SubItems.Add(m_BookTransactionService.IsAlreadyReturn(trans)?"YES":"NO");
            item.Tag = trans;
        }

        private void listView_DoubleClick(object sender, EventArgs e)
        {
            if (listView.SelectedItem != null)
            {
                BookTransaction BookStock = (BookTransaction)listView.SelectedItem.Tag;
                FormBookBorrow frm = new FormBookBorrow(BookStock, m_currentUser, this);
                frm.ShowDialog();
            }
        }

        internal void UpdateData(BookTransaction BookStock)
        {
            initData();
            //listView.SelectedItem.SubItems[0].Text = BookStock.Code;
            //listView.SelectedItem.SubItems[1].Text = BookStock.TransactionDate.ToString("dd-MMM-yyyy");
            //listView.SelectedItem.SubItems[2].Text = BookStock.Name;
            //listView.SelectedItem.SubItems[3].Text = BookStock.TransactionType.ToString();
            //listView.SelectedItem.SubItems[4].Text = BookStock.Quantity.ToString();
            //listView.SelectedItem.SubItems[4].Text = BookStock.Remark;
            //listView.SelectedItem.Tag = BookStock;
        }

        private void listView_SelectedIndexChanged(object sender, EventArgs e)
        {
            toolBarButtonDelete.Enabled = listView.SelectedItem != null;
            toolBarButtonReturn.Enabled = listView.SelectedItem != null;
        }

        private void buttonFInd_Click(object sender, EventArgs e)
        {
            initData();
        }

        private void textBoxBookCode_EnterKeyDown(object objSender, KeyEventArgs objArgs)
        {
            if(objArgs.KeyData == Keys.Enter)
                initData();
        }
    }
}